package cn.zcx;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.sql.*;

import static java.lang.Class.forName;

/**
 * description:
 * author:zcx
 * lenovo
 * 时间：2022.02.28.19.37
 */
public class Demo1 {
    @Test
    @DisplayName("mysql5链接")
    void m5(){
        //01加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //02链接数据库
            //Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/","root","");
            // Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","");
            //System.out.println(conn);
            Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/db1?useUnicode=true$charactorEncoding=utf8&useSSL=false","root","");
            //03 执行SQL语句
            Statement st=conn.createStatement();
            //st.execute("create database db1 default charset utf8");
            //st.executeUpdate("use db1");
            //st.execute("create table stu (id int unsigned auto_increment,name varchar(30),primary key(id))");
            //st.executeUpdate("select *from stu");
           // int row=st.executeUpdate("insert into stu values(null,'张三'),(null,'李四'),(null,'王五')");
            ResultSet rs=st.executeQuery("select *from stu");
            if(rs.isBeforeFirst()){
                while(rs.next()){
                    System.out.println(rs.getInt(1)+" "+ rs.getString("name"));
                }
            }else {
            System.out.println("没有数据");}
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }

    }
    @Test
    @DisplayName("mysql8链接")
    void m8(){
        String dirver="com.mysql.cj.jdbc.Driver";
        String d="Jdbc:mysql://localhost:3306?";
        String uid="root";
        String pwd="";
        //01 建立驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn =DriverManager.getConnection("Jdbc:mysql://localhost:3306/db1","root","");
            Statement st=conn.createStatement();
            int row=st.executeUpdate("delete from stu where id='1'");
            ResultSet rs=st.executeQuery("select *from stu");
            System.out.println(row);
            while(rs.next()){
                System.out.println(rs.getInt(1)+""+rs.getString("name"));
            }
            conn.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
